<template>
  <el-button v-if="fromPath" size="mini" @click="goBack">{{ $t('返回主界面') }}</el-button>
</template>
<script>
import Cookies from 'js-cookie'
export default {
    name: 'BackTop',
    data() {
        return {
            fromPath: null
        }
    },
    watch: {
        '$route': {
            handler(val) {
                if (Cookies.get('paths')) {
                    const nPaths = JSON.parse(Cookies.get('paths')) || {}
                    // console.log('arr', nPaths)
                    if (nPaths.arr.length > 0) {
                        var keys = []
                        nPaths.arr.forEach(v => {
                            keys = Object.keys(v.query)
                        })
                        // console.log(keys)
                        var obj = {}
                        for (var i in keys) {
                            obj[keys[i]] = val.query[keys[i]]
                        }
                        // console.log(obj)
                        var info = nPaths.arr.find(item => JSON.stringify(item.query) === JSON.stringify(obj))
                        // console.log(info)
                        if (info) {
                            this.fromPath = info.fromPath
                        }
                    }
                }
            },
            deep: true,
            immediate: true
        }
    },
    methods: {
        goBack() {
            if (this.fromPath) {
                window.open(this.fromPath, '_self')
            }
        }
    }
}
</script>
<style lang="scss" scoped>

</style>
